#######################################################
#A COMMON-SPACE TECHNIQUE FOR VOTERS AND LEGISLATORS
#WITH APPLICATION TO THE 2010/2011 Elections
####################################################### 

##NB - Load the Master file first



##We extract each unique district
cd <- paste(V206,"-d-",V276,sep="")
cd_unique <- unique(cd)

##create a new cces variable for house scaling
cces$house_scale <- NULL


##not that the placement variable for the house incumbent is coded oddly
house_inc <- replace(as.numeric(cces$CC334L), as.numeric(cces$CC334L)>7 | is.na(cces$CC334L), 999)
house_r <- replace(as.numeric(cces$CC334K), as.numeric(cces$CC334K)>7 | is.na(cces$CC334K), 999)
house_d <- replace(as.numeric(cces$CC334J), as.numeric(cces$CC334J)>7| is.na(cces$CC334J), 999)

cces$house <- (house_inc>7)*((cces$V502=="Republican")*house_r + (cces$V502=="Democratic")*house_d) + (house_inc<=7)*house_inc

####################################################
#To the matrix of house members, we add individuals,
#parties, and the president
####################################################

self <- CC334A
obama <- CC334C
democrats <- CC334D
republicans <- CC334E

placement_matrix <- cbind(self, obama, democrats, republicans)

for (j in 1:ncol(placement_matrix)){
  placement_matrix[,j] <- as.numeric(placement_matrix[,j])
  placement_matrix[,j] <- replace(placement_matrix[,j], placement_matrix[,j]==8 | is.na(placement_matrix[,j]), 999)
}  

###################################################
#Next, we estimate AM step-by-step for each district
###################################################

##We need one overall estimate across all respondents
overall_scale <- aldmck(placement_matrix[,1:4], polarity=2, respondent=1, missing=999, verbose=TRUE)

#save the overall dem and rep pos
dem_overall <- overall_scale$stim[2]
rep_overall <- overall_scale$stim[3]


##here are the voters' preferences
ideal_points <- overall_scale$resp$ideal

#now house members
placement_holder <- NULL
med_voter <- NULL

for (i in 1:length(cd_unique)){
  
  #subset by district
  tmp <- subset(cces, cd==cd_unique[i])
  temp <- cbind(placement_matrix[cd==cd_unique[i],], tmp$house)
  
  #remove stimuli that are not measured
  drop <- NULL
  for (j in 1:ncol(temp)) if(sum(temp[,j]==999, na.rm=TRUE)==nrow(temp)) drop <- c(drop, j)
  if (sum(drop)>0) temp <- temp[,-drop]
  
  scaling <- aldmck(temp, polarity=2, respondent=1, missing=999, verbose=TRUE)
  
  #recale from state space back to overall space
  #using the equ OVERALL = slope*STATE - slope*STATE_DEM + OVERALL_DEM
  
  slope <- (overall_scale$stim[3]-overall_scale$stim[2])/(scaling$stim[3]-scaling$stim[2])
  rescaling <- slope*scaling$stim - slope*scaling$stim[2] + overall_scale$stim[2]
  
  #add results to cces dataset...
  cces$house_scale[cd==cd_unique[i]] <- rescaling[4]
  
  #save results
  placement_holder <- c(placement_holder, rescaling[4])
  med_voter <- c(med_voter, mean(ideal_points[cd==cd_unique[i]],na.rm=TRUE))
  
  cat(cd_unique[i], " done", "\n")
}

names(placement_holder) <- cd_unique

##Plot the results
plot(1:length(placement_holder), placement_holder, pch="")
text(1:length(placement_holder), placement_holder, names(placement_holder), cex=0.7)


##append the district mean preferences
dist_ideal_pt <- tapply(ideal_points, cd, mean, na.rm=TRUE)
text(1:length(dist_ideal_pt), dist_ideal_pt, names(dist_ideal_pt), cex=0.7)


##Group and save the results by district
house_results <- cbind(placement_holder, med_voter)
